package com.qf.fmall.shopcart.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.product.entity.ProductSku;
import com.qf.fmall.product.mapper.ProductSkuMapper;
import com.qf.fmall.shopcart.entity.ShoppingCart;
import com.qf.fmall.shopcart.mapper.ShoppingCartMapper;
import com.qf.fmall.shopcart.service.IShoppingCartService;
import com.qf.fmall.vo.ResultVo;
import javafx.scene.input.DataFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author weijy
 * @since 2024-01-17
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/shopcart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private IShoppingCartService shoppingCartService;


    @RequestMapping("/listbycids")
    public ResultVo listByCids(@RequestParam List<String> cids){

        List<ShoppingCart> cartList  = shoppingCartService.getListByCids(cids);

        return new ResultVo(10000, "success", cartList);
    }


    @PutMapping("/update/{id}/{sum}")

    public ResultVo update(@PathVariable("id") String id, @PathVariable("sum") String sum) {

        ShoppingCart cart = new ShoppingCart();
        cart.setCartId(Integer.valueOf(id));
        cart.setCartNum(sum);

        shoppingCartMapper.updateById(cart);

        return new ResultVo(10000, "success", null);

    }


    @RequestMapping("/list")
    public ResultVo getUserList(Integer userId) {

        List<ShoppingCart> list =shoppingCartService.getListByUserId(userId);

        return new ResultVo(10000, "success", list);
    }


    @RequestMapping("/add")
    public ResultVo add(@RequestBody ShoppingCart shoppingCart) {

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(date);
        shoppingCart.setCartTime(time);
        shoppingCartService.save(shoppingCart);

        return new ResultVo(10000,"success",null);


    }


}
